package com.huawei.ceiling.slice;

import com.huawei.ceiling.BaseProvider;
import com.huawei.ceiling.ResourceTable;
import com.huawei.ceiling.StickyBean;
import com.huawei.ceiling.StickyViewHolder;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        init();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    public void init() {
        ListContainer showLc = (ListContainer) findComponentById(ResourceTable.Id_showLc);

        BaseProvider provider = new BaseProvider(getContext(), showLc, createList(), ResourceTable.Layout_item_string);
        provider.setComponentBindingListener(new BaseProvider.ComponentBindingListener() {
            @Override
            public StickyViewHolder creatViewHolder(Component item) {
                return new StickyViewHolder(item);
            }

            @Override
            public void initData(StickyViewHolder viewHolder, StickyBean object, int position) {
                viewHolder.updateView(object);
            }
        });
        provider.setStickyListener(object -> ((StickyBean) object).getType() == 0 ? true : false);
        showLc.setItemProvider(provider);
    }

    //生成数据
    public List<StickyBean> createList() {
        List<StickyBean> stickyBeans = new ArrayList<>();
        for (int i = 0; i < 53; i++) {
            stickyBeans.add(new StickyBean("Item " + i, 0));
        }
        stickyBeans.add(0, new StickyBean("Header " + 0, 1));
        stickyBeans.add(7, new StickyBean("Header " + 1, 1));
        stickyBeans.add(14, new StickyBean("Header " + 2, 1));
        stickyBeans.add(21, new StickyBean("Header " + 3, 1));
        stickyBeans.add(28, new StickyBean("Header " + 4, 1));
        stickyBeans.add(35, new StickyBean("Header " + 5, 1));
        stickyBeans.add(42, new StickyBean("Header " + 6, 1));
        stickyBeans.add(49, new StickyBean("Header " + 7, 1));
        stickyBeans.add(56, new StickyBean("Header " + 8, 1));

        return stickyBeans;
    }
}
